//
//  BKLichThiViewController.m
//  I'm BKer
//
//  Created by Nguyen Van Hoa on 5/14/13.
//  Copyright (c) 2013 vanhoavn. All rights reserved.
//

#import "BKLichThiViewController.h"
#import "BKLichThiCell.h"
@interface BKLichThiViewController ()

@end

@implementation BKLichThiViewController

- (id)init
{
    return [self initWithStyle:UITableViewStylePlain];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title = @"Lịch thi";
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSArray* arr = [BKStudentManager danhSachLichThi];
    NSMutableArray* mDataMu = [[NSMutableArray alloc] init];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
    NSString* today = [NSString stringWithFormat:@"%04d%02d%02d",[components year],[components month],[components day]];
    for(NSDictionary* dict in arr){
        NSString* ngay1 = [dict objectForKey:@"ngay1"];
        if([ngay1 length]==8 && [ngay1 compare:today]!=NSOrderedAscending){
            [mDataMu addObject:[NSDictionary dictionaryWithObjectsAndKeys:
             [NSString stringWithFormat:@"%c%c/%c%c/%c%c%c%c"
                      ,[ngay1 characterAtIndex:6]
                      ,[ngay1 characterAtIndex:7]
                      ,[ngay1 characterAtIndex:4]
                      ,[ngay1 characterAtIndex:5]
                      ,[ngay1 characterAtIndex:0]
                      ,[ngay1 characterAtIndex:1]
                      ,[ngay1 characterAtIndex:2]
                      ,[ngay1 characterAtIndex:3]],@"ngay",
             [dict objectForKey:@"ten"], @"monhoc",
             [NSString stringWithFormat:@"%@ - %@",
                            [dict objectForKey:@"maso"],
                            [dict objectForKey:@"nhom"]],@"mamonnhom", 
             [NSString stringWithFormat:@"Tiết %@ - %@",
                                  [dict objectForKey:@"tiet1"],
                                  [dict objectForKey:@"phong1"]
                                  ],@"thoigiandiadiem",
            [NSString stringWithFormat:@"%@ %02d",
                        [dict objectForKey:@"ngay1"],
                        [[dict objectForKey:@"tiet1"] integerValue]
            ], @"sort",
             nil]];
        }
        NSString* ngay2 = [dict objectForKey:@"ngay2"];
        if([ngay2 length]==8 && [ngay2 compare:today]!=NSOrderedAscending){
            [mDataMu addObject:[NSDictionary dictionaryWithObjectsAndKeys:
             [NSString stringWithFormat:@"%c%c/%c%c/%c%c%c%c"
                      ,[ngay2 characterAtIndex:6]
                      ,[ngay2 characterAtIndex:7]
                      ,[ngay2 characterAtIndex:4]
                      ,[ngay2 characterAtIndex:5]
                      ,[ngay2 characterAtIndex:0]
                      ,[ngay2 characterAtIndex:1]
                      ,[ngay2 characterAtIndex:2]
                      ,[ngay2 characterAtIndex:3]],@"ngay",
             [dict objectForKey:@"ten"],@"monhoc", 
             [NSString stringWithFormat:@"%@ - %@",
                            [dict objectForKey:@"maso"],
                            [dict objectForKey:@"nhom"]],@"mamonnhom", 
             [NSString stringWithFormat:@"Tiết %@ - %@",
                                  [dict objectForKey:@"tiet2"],
                                  [dict objectForKey:@"phong2"]
                                  ],@"thoigiandiadiem", 
            [NSString stringWithFormat:@"%@ %02d",
             [dict objectForKey:@"ngay2"],
             [[dict objectForKey:@"tiet2"] integerValue]
             ], @"sort",
             nil]];
        }
    }
    mData = [mDataMu sortedArrayWithOptions:NSSortStable
                  usingComparator:^NSComparisonResult(NSDictionary* d1, NSDictionary* d2){
                      return [[d1 objectForKey:@"sort"] compare:[d2 objectForKey:@"sort"]];
                  }
     ];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0) return [mData count];
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"BKLichThiCell";
    BKLichThiCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:nil options: nil] objectAtIndex:0];
    }
    NSDictionary* dict = [mData objectAtIndex:indexPath.row];
    if(dict!=NULL){
        cell.lblNgay.text = [dict objectForKey:@"ngay"];
        cell.lblMaMonNhom.text = [dict objectForKey:@"mamonnhom"];
        cell.lblMonhoc.text = [dict objectForKey:@"monhoc"];
        cell.lblThoigianDiadiem.text = [dict objectForKey:@"thoigiandiadiem"];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section!=0) return 0;
    return 82;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
